package com.hz.filemanager.ui.file.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.hz.filemanager.R;
import com.hz.filemanager.utils.AppUtils;
import com.hz.filemanager.utils.FileUtils;
import com.kathline.library.util.ZFileHelp;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * 以
 * @see File
 * 作为数据bean，动态加载当前文件类型icon
 */
public class DownloadAdapter extends RecyclerView.Adapter<DownloadAdapter.ViewHolder> {

    private Context context;
    private List<File> fileBeans;
    private List<File> selectedFiles = new ArrayList<>();

    private OnRVItemClickListener listener;

    private boolean selectMode = false;

    public DownloadAdapter(Context context, List<File> fileBeans, OnRVItemClickListener listener) {
        this.context = context;
        this.fileBeans = fileBeans;
        this.listener = listener;
    }

    /**
     * 关闭选择模式并清除历史选择
     * @param selectMode 模式
     */
    @SuppressLint("NotifyDataSetChanged")
    public void setSelectMode(boolean selectMode) {
        this.selectMode = selectMode;
        selectedFiles.clear();
        notifyDataSetChanged();
    }

    public void checkAllOf(boolean check){
        selectedFiles.clear();
        if (check){
            selectedFiles.addAll(fileBeans);
        }
        DownloadAdapter.this.notifyItemRangeChanged(0,fileBeans.size());
    }

    @SuppressLint("NotifyDataSetChanged")
    public void resetData(List<File> fileBeans) {
        this.fileBeans = fileBeans;
        notifyDataSetChanged();
    }

    public List<File> getSelectedFiles() {
        return selectedFiles;
    }

    public boolean isSelectMode() {
        return selectMode;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.docs_rv_item, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        final File fileBean = fileBeans.get(position);

        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (listener != null && !isSelectMode()){
                    listener.onLongClick();
                }
                return true;
            }
        });

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (isSelectMode()) {
                    holder.cb.toggle();
                    if (holder.cb.isChecked()) {
                        selectedFiles.add(fileBean);
                    } else {
                        selectedFiles.remove(fileBean);
                    }
                }

                if(listener != null){
                    listener.onClick(v, position,fileBean,false);
                }
            }
        });

        if (selectMode){
            holder.cb.setVisibility(View.VISIBLE);
            holder.cb.setChecked(selectedFiles.contains(fileBean));
        }else {
            holder.cb.setVisibility(View.INVISIBLE);
        }

        //当作为文件夹时，显示此文件夹包含的目录条目数
        if (fileBean.isDirectory()){
            Glide.with(context)
                    .load(R.mipmap.ic_zfile_folder)
                    .into(holder.iv);
            holder.name.setText(fileBean.getName());
            holder.size.setText(fileBean.list().length+" items");
            holder.date.setText("");
        }else {
            //当作为一个文件时，作为普通文件显示文件名，大小，最后保存时间等
            //作为apk文件时，icon显示apk图标
            //作为图片文件时，icon显示图片
            //作为视频时，icon显示视频缩略图
            final String extName = FileUtils.getFileExtension(fileBean.getAbsoluteFile());
            final int type = com.hz.filemanager.model.file.FileUtils.getFileTypeOfSort(fileBean.getAbsolutePath());
            if ("apk".equals(extName)){
                Glide.with(context)
                        .load(AppUtils.getApkIcon(context, fileBean.getAbsolutePath()))
                        .into(holder.iv);
            }else if ("mp3".equals(extName)){
                Glide.with(context)
                        .load(fileBean.getPath())
                        .into(holder.iv);
            }else if ("mp4".equals(extName)){
                Glide.with(context)
                        .load(Uri.fromFile(fileBean))
                        .into(holder.iv);
            }else if (type == com.hz.filemanager.model.file.FileUtils.TYPE_DOC){
                Glide.with(context)
                        .load(R.mipmap.doc)
                        .into(holder.iv);
            }else if (type == com.hz.filemanager.model.file.FileUtils.TYPE_ZIP){
                Glide.with(context)
                        .load(R.mipmap.zip)
                        .into(holder.iv);
            }else if (type == com.hz.filemanager.model.file.FileUtils.TYPE_TXT){
                Glide.with(context)
                        .load(R.mipmap.txt)
                        .into(holder.iv);
            }else if (type == com.hz.filemanager.model.file.FileUtils.TYPE_XLS){
                Glide.with(context)
                        .load(R.mipmap.xls)
                        .into(holder.iv);
            }else if (type == com.hz.filemanager.model.file.FileUtils.TYPE_PPT){
                Glide.with(context)
                        .load(R.mipmap.ppt)
                        .into(holder.iv);
            } else if (type == com.hz.filemanager.model.file.FileUtils.TYPE_PDF){
                Glide.with(context)
                        .load(R.mipmap.pdf)
                        .into(holder.iv);
            } else if (type == com.hz.filemanager.model.file.FileUtils.TYPE_OTHER){
                Glide.with(context)
                        .load(R.mipmap.icon_splash)
                        .into(holder.iv);
            } else {
                Glide.with(context)
                        .load(R.mipmap.icon_splash)
                        .into(holder.iv);
            }
            holder.name.setText(fileBean.getName());
            holder.size.setText(ZFileHelp.getFileSize(fileBean.getAbsolutePath()));
            holder.date.setText(ZFileHelp.getFormatFileDate(fileBean));
        }
    }

    @Override
    public int getItemCount() {
        return fileBeans.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView iv;
        private TextView name;
        private TextView size;
        private TextView date;
        private CheckBox cb;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            iv = (ImageView) itemView.findViewById(R.id.iv);
            name = (TextView) itemView.findViewById(R.id.name);
            size = (TextView) itemView.findViewById(R.id.size);
            date = (TextView) itemView.findViewById(R.id.date);
            cb = (CheckBox) itemView.findViewById(R.id.cb);
        }
    }

}
